import { createRouter, createWebHistory } from 'vue-router'
import HomeView from '../views/HomeView.vue'
import { loginStore } from '@/stores/login'
import { getRoute } from '@/composables/menu'



const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      name: 'Home',
      component: HomeView,
      children: getRoute()
    },  
    {
      path: '/login',
      name: 'Login',
      components: {
        LoginPage: ()=>import('@/views/LoginView.vue')
      }
    }
  ]
})

router.beforeEach((to, from)=>{
  const store = loginStore()
  if(!store.user.userName && to.path !== '/login'){
    return {name: 'Login'}
  }
})

export default router
